package org.example.controller.backup;

import org.example.common.result.ResponseData;
import org.example.flowable.handler.InstanceHandler;
import org.example.flowable.handler.TaskHandler;
import org.example.flowable.handler.TaskQueryHandler;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>
 * 流程任务相关
 * </p>
 *
 * @author liuxz
 * @since 2019-08-20
 */
@RestController
@RequestMapping("api/flow/task")
public class TaskController {

    protected static Logger logger = LoggerFactory.getLogger(InstanceHandler.class);
    @Autowired
    private TaskHandler taskHandler;
    @Autowired
    private TaskQueryHandler taskQueryHandler;


    @RequestMapping(value = "/execute", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData executeTask(String taskId, @RequestBody Map<String, Object> variables) throws Exception {
        Map<String, Object> map = taskHandler.complete(taskId, variables, false);

        return ResponseData.success(map);
    }


    @RequestMapping(value = "/claim", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData claim(String taskId, String userId) throws Exception {
        taskHandler.claim(taskId, userId);
        return ResponseData.success("任务签收成功");
    }


    @RequestMapping(value = "/unclaim", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData unclaim(String taskId) throws Exception {
        taskHandler.unclaim(taskId);
        return ResponseData.success("任务反签收成功");
    }


    @RequestMapping(value = "/delegate", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData delegate(String taskId, String userId) throws Exception {
        taskHandler.delegate(taskId, userId);
        return ResponseData.success("任务委派成功");
    }


    @RequestMapping(value = "/resolve", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData<Task> reslove(String taskId) throws Exception {
        taskHandler.resolveTask(taskId);
        return ResponseData.success();
    }


    @RequestMapping(value = "/assignee ", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData assignee(String taskId, String userId) throws Exception {
        taskHandler.setAssignee(taskId, userId);
        return ResponseData.success("任务转办成功");
    }

    @RequestMapping(value = "/comment/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData<Comment> addComment(String taskId, String instanceId, String message) throws Exception {
        Comment comment = taskHandler.addComment(taskId, instanceId, message);
        return ResponseData.success(comment);
    }
}

